package com.imooc.user.thriftServer;

import com.imooc.thrift.user.UserService;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Auther: zhaozeyang
 * @Date: 2018/8/18 18:11
 * @Description:
 */
@Component
public class ThriftServerImpl {

    @Value("${service.port}")
    private int servicePort;

    @Value("${service.name}")
    private String serviceName;

    @Autowired
    private UserService.Iface userService;


    @PostConstruct
    public void startThriftServer() {
        TProcessor tProcessor = new UserService.Processor<>(userService);//处理器
        TNonblockingServerSocket socket = null;
        try {
            socket = new TNonblockingServerSocket(servicePort);  //socket链接
        } catch (TTransportException e) {
            e.printStackTrace();
        }

        TNonblockingServer.Args args = new TNonblockingServer.Args(socket);  //构建server启动参数
        args.processor(tProcessor);  //处理器
        args.transportFactory(new TFramedTransport.Factory()); //传输方式
        args.protocolFactory(new TBinaryProtocol.Factory()); //二进制序列化方式

        TServer tServer = new TNonblockingServer(args);// 非阻塞IO
        tServer.serve();

    }


}
